package com.techwells.learningkites.controller;

import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.techwells.learningkites.R;
import com.techwells.learningkites.adapter.CourseVideoListAdapter;
import com.techwells.learningkites.model.MainPageViewModel;
import com.techwells.learningkites.model.VideoListViewModel;
import com.techwells.learningkites.networkservice.KitesServiceMediator;
import com.techwells.learningkites.networkservice.model.Lesson;
import com.techwells.learningkites.networkservice.model.LessonVideo;
import com.techwells.learningkites.networkservice.model.Video;
import com.techwells.taco.base.CommonBaseActivity;
import com.techwells.taco.mvvm.Route;
import com.techwells.taco.tasktool.TaskToken;

public class VideoListActivity extends CommonBaseActivity implements OnItemClickListener {
	
	private ListView lvVideos;
	private CourseVideoListAdapter adapter;
	private VideoListViewModel presentModel;
	private List<Lesson> videoList;
	
	private boolean isRefresh = true;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_videolist);
		
		initTitleBar(R.string.course, defaultLeftClickListener);
		initViews();
	}

	private void initViews() {
		lvVideos = (ListView) findViewById(R.id.lv_videos);
		
	}
	
	@Override
	public void alreadyBindBaseViewModel() {
		// TODO Auto-generated method stub
		super.alreadyBindBaseViewModel();
		presentModel = (VideoListViewModel) baseViewModel;
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if (isRefresh) {
			doRequestGetLessons();
		}
		
	}

	private void doRequestGetLessons() {
		int courseId = getIntent().getIntExtra("courseId", 0);
		HashMap<String, Object> argMap = new HashMap<String, Object>();	
		argMap.put("courseId", courseId);
		doTask(KitesServiceMediator.SERVICE_GET_LESSONS,argMap);
	}
	
	
	@Override
	public void refreshData(TaskToken token) {
		// TODO Auto-generated method stub
		super.refreshData(token);
		
		String method = token.method;
		if (method.equals(KitesServiceMediator.SERVICE_GET_LESSONS)) {
			videoList = ((VideoListViewModel)baseViewModel).videoList;
			adapter = new CourseVideoListAdapter(this, videoList);
			lvVideos.setAdapter(adapter);
			lvVideos.setOnItemClickListener(this);
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		Intent intent = new Intent(this, VideoDetailActivity.class);
		intent.putExtra("videoId", videoList.get(arg2).videoSerial.videoId);
		Route.route().nextControllerWithIntent(this, VideoDetailActivity.class.getName(), Route.WITHOUT_RESULTCODE,intent);
		
	}
}
